<?php
class TetTetTetController extends Controller {
        
    function __construct() {
        
        if (isset($_SESSION['userID'])) {
            $login = new Login();
            if (!$login->get($_SESSION['userID']))
                header("Location: ".SITE_URL.'?c=Login');
        }
        else {
            header("Location: ".SITE_URL.'?c=Login');
        }
        
        parent::__construct();
        
    }
    
	function index() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $this->vars['section'] = "TetTetTet";
            $this->vars['title'] = "List of questions and answers of 'Tet Tet Tet'";
            
            $array_menus = array();
        
            $filter = array();
            $filter['parent_id'] = 0;
            $filter['type'] = 1;
            Menu::getMenuList($array_menus, $filter);
            
            $this->vars['array_menus'] = $array_menus;
            
            $question = new Question();
            $question->addSelect();
            $question->addSelect("question.id question_id,
                                    question.title question_title,
                                    answer.id answer_id,
                                    answer.content answer_content,
                                    answer.is_correct answer_correct,
                                    question.image");
            $question->addJoin(new Answer(), 'LEFT');
            $question->addWhere("question_type_id = 3");
            $question->orderBy("question.title ASC, answer.content ASC");
            $question->find();
            
            $this->vars['question'] = $question;
            
            $this->display('TetTetTet_list');
        }
        
        function uploadPicture($name, $overwrite = false) {
            if ((($_FILES["image"]["type"] == "image/gif")
                || ($_FILES["image"]["type"] == "image/jpeg")
                || ($_FILES["image"]["type"] == "image/pjpeg"))
                && ($_FILES["image"]["size"] < 1000000))
            {

				$size = getimagesize($_FILES["image"]["tmp_name"]);

				if ($size[0] > 640) {
					MessageHandler::add("Invalid file size");
					return false;
				}
                
                $extension = explode(".", $_FILES["image"]["name"]);
                $extension = $extension[count($extension) - 1];
                
                if ($_FILES["image"]["error"] > 0) {
                    MessageHandler::add("Return Code: " . $_FILES["image"]["error"]);
                }
                else {
                    
                    if (file_exists(APPLICATION_PATH."/../images/" . $name.'.'.$extension) && !$overwrite)
                    {
                        MessageHandler::add("The file is already existed.");
                    }
                    else {
                        move_uploaded_file($_FILES["image"]["tmp_name"], APPLICATION_PATH."/../images/" . $name.'.'.$extension);
                    }
                }
            }
            else
            {
                MessageHandler::add("Invalid file");
            }
            
            if (MessageHandler::countError() > 0)
                return false;
            else
                return $name.'.'.$extension;
        }
        
        function addQuestion() {
            
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=TetTetTet',
            _LB_ADD_QUESTION => SITE_URL.'?c=TetTetTet&a=addQuestion'));
            
            $this->vars['section'] = "TetTetTet";
            $this->vars['title'] = "Add a question of 'Tet Tet Tet'";
            $this->vars['backlink'] = SITE_URL.'?c=TetTetTet';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            
            if ($act == ACT_VALID_EDIT) {
                
                $question->title = isset($_POST['title']) ? $_POST['title'] : '';
                $question->question_type_id = 3;
				
				if (!$_FILES["image"]['name'] || $_FILES["image"]['name'] == '')
					$question->image = 'no_picture';
                
                if ($question->validateInput()) {
                    
                    $id = $question->insert();
                    
                    if ($id) {
						if ($_FILES["image"]['name'] && $_FILES["image"]['name'] != '') {
							if ($file = $this->uploadPicture("1_".$id)) {
								$question = new Question();
								$question->get($id);
								$question->image = $file;
								$question->update();
							}
							else {
								$question = new Question();
								$question->get($id);
								$question->delete();
							}
						}

                        if (MessageHandler::countError() == 0)
                            header("Location: ".SITE_URL.'?c=TetTetTet');
                    }
                    
                }
                
            }

            $this->vars['question'] = $question;
            
            $this->display('TetTetTet_form');
        }
        
        function editQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $id = $_GET['id'];
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=TetTetTet',
            _LB_ADD_QUESTION => SITE_URL.'?c=TetTetTet&a=editQuestion&id='.$id));
            
            $this->vars['section'] = "TetTetTet";
            $this->vars['title'] = "Update an existing question of 'Tet Tet Tet'";
            $this->vars['backlink'] = SITE_URL.'?c=TetTetTet';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            $question->get($id);
            
            if ($act == ACT_VALID_EDIT) {
                
                $question->title = isset($_POST['title']) ? $_POST['title'] : '';
                $question->question_type_id = 3;
                
                if ($_FILES["image"]["name"]) {
                    if ($file = $this->uploadPicture("1_".$id, true)) {
                        $question->image = $file;
                    }
                }
                
                if ($question->validateInput()) {
                    if ($question->update()) {
                        header("Location: ".SITE_URL.'?c=TetTetTet');
                    }
                }
                
            }

            $this->vars['question'] = $question;
            
            $this->display('TetTetTet_form');
        }
        
        function deleteQuestion() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $id = $_GET['id'];
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question = new Question();
            
            if (!$question->get($id))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $question->delete();
            
            header("Location: ".SITE_URL.'?c=TetTetTet');
        }
        
        function addAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=TetTetTet',
            _LB_ADD_ANSWER => SITE_URL.'?c=TetTetTet&a=addAnswer'));
            
            $this->vars['section'] = "TetTetTet";
            $this->vars['title'] = "Add an answer of a question of 'Tet Tet Tet'";
            $this->vars['backlink'] = SITE_URL.'?c=TetTetTet';
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question_id = null;
            
            if (isset($_REQUEST['question_id']))
                $question_id = $_REQUEST['question_id'];
            
            $is_correct = isset($_POST['is_correct']) ? $_POST['is_correct'] : 0;
            
            $answer = array(new Answer(), new Answer(), new Answer(), new Answer());
            
            if ($act == ACT_VALID_EDIT) {
                
                if (!isset($question_id))
                    MessageHandler::add ('Missing question ID');
                
                // Set answer 0
                $answer[0]->content = isset($_POST['content0']) ? $_POST['content0'] : '';
                $answer[0]->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $question_id = $answer[0]->question_id;
                $answer[0]->is_correct = $is_correct == 0 ? 1 : 0;
                
                if ($answer[0]->validateInput())
                    $ok = true;
                else
                    $ok = false;
                
                // Set answer 1
                $answer[1]->content = isset($_POST['content1']) ? $_POST['content1'] : '';
                $answer[1]->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $question_id = $answer[1]->question_id;
                $answer[1]->is_correct = $is_correct == 1 ? 1 : 0;
                
                if ($answer[1]->validateInput())
                    $ok = true;
                else
                    $ok = false;
                
                // Set answer 2
                $answer[2]->content = isset($_POST['content2']) ? $_POST['content2'] : '';
                $answer[2]->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $question_id = $answer[2]->question_id;
                $answer[2]->is_correct = $is_correct == 2 ? 1 : 0;
                
                if ($answer[2]->validateInput())
                    $ok = true;
                else
                    $ok = false;
                
                // Set answer 3
                $answer[3]->content = isset($_POST['content3']) ? $_POST['content3'] : '';
                $answer[3]->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $question_id = $answer[3]->question_id;
                $answer[3]->is_correct = $is_correct == 3 ? 1 : 0;
                
                if ($answer[3]->validateInput())
                    $ok = true;
                else
                    $ok = false;
                
                if ($ok) {
                    $answer[0]->insert();
                    $answer[1]->insert();
                    $answer[2]->insert();
                    $answer[3]->insert();
                    header("Location: ".SITE_URL.'?c=TetTetTet');
                }
            }
            
            $question = new Question();
            $question->addSelect();
            $question->addSelect("question.id, question.title");
            $question->addWhere("question.question_type_id = 3");
            $question->orderBy("question.title ASC");
            $question->find();

            $this->vars['question'] = $question;
            $this->vars['answer'] = $answer;
            $this->vars['question_id'] = $question_id;
            
            $this->display('TetTetTet_Answer_form');
        }
        
        function editAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            $cfer = new Cfer(array(
            _LB_QUESTION => SITE_URL.'?c=TetTetTet',
            _LB_ADD_ANSWER => SITE_URL.'?c=TetTetTet&a=editAnswer'));
            
            $this->vars['section'] = "TetTetTet";
            $this->vars['title'] = "Edit an existing answer of a question of 'Tet Tet Tet'";
            $this->vars['backlink'] = SITE_URL.'?c=TetTetTet';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $id = $_GET['id'];
            
            $act = isset($_POST['act']) ? $_POST['act'] : false;
            
            $question_id = null;
            
            if (isset($_REQUEST['question_id']))
                $question_id = $_REQUEST['question_id'];
            
            $answer = new Answer();
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $question_id = $answer->question_id;
            
            if ($act == ACT_VALID_EDIT) {
                
                if (!isset($question_id))
                    MessageHandler::add ('Missing question ID');
                
                $answer->content = isset($_POST['content']) ? $_POST['content'] : '';
                $answer->question_id = isset($_REQUEST['question_id']) ? $_REQUEST['question_id'] : null;
                $question_id = $answer->question_id;
                $answer->is_correct = isset($_POST['is_correct']) ? 1 : 0;
                
                if ($answer->validateInput()) {
                    if ($answer->update()) {
                        if ($answer->is_correct) {
                            $answer = new Answer();
                            $answer->query("UPDATE answer SET is_correct = 0 WHERE question_id = $question_id AND id <> $id");
                        }
                        header("Location: ".SITE_URL.'?c=TetTetTet');
                    }
                }
                
            }
            
            $question = new Question();
            $question->addSelect();
            $question->addSelect("question.id, question.title");
            $question->addWhere("question.question_type_id = 3");
            $question->orderBy("question.title ASC");
            $question->find();

            $this->vars['question'] = $question;
            $this->vars['answer'] = $answer;
            $this->vars['question_id'] = $question_id;
            
            $this->display('TetTetTet_Answer_form');
        }
        
        function deleteAnswer() {
            include APPLICATION_PATH.'/helpers/utils_helper.php';
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $id = $_GET['id'];
            
            $answer = new Answer();
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $answer->delete();
            
            header("Location: ".SITE_URL.'?c=TetTetTet');
        }
        
        function setCorrect() {
            $answer = new Answer();
            
            if (!isset($_GET['id']))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $id = $_GET['id'];
            
            if (!$answer->get($id))
                header("Location: ".SITE_URL.'?c=TetTetTet');
            
            $question_id = $answer->question_id;
            $answer->is_correct = 1;
            $answer->update();
            
            $answer = new Answer();
            $answer->query("UPDATE answer SET is_correct = 0 WHERE question_id = $question_id AND id <> $id");
            
            header("Location: ".SITE_URL.'?c=TetTetTet');
        }
        
}

